/*
 * @(#)JGraphGraphvizEncoder.java 1.0 12-MAY-2004
 * 
 * Copyright (c) 2001-2005, Gaudenz Alder and van woods
 * All rights reserved. 
 * 
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
 */
package com.jgraph.codecplugin;

import java.text.DateFormat;
import java.util.Date;
import java.util.Hashtable;
import java.util.Iterator;

import org.jgraph.JGraph;
import org.jgraph.graph.GraphModel;

/**
 * Graphviz Codec.
 */
public class JGraphpadGraphvizCodec {

	static transient Hashtable hash;

	public static String encode(JGraph graph, Object[] cells) {
		hash = new Hashtable();
		DateFormat dateformat = DateFormat.getDateTimeInstance(DateFormat.LONG,
				DateFormat.LONG);
		String date = dateformat.format(new Date());
		StringBuffer gv = new StringBuffer("/* Graphviz file generated by "
				+ "JGraph - " + date + " */" + "\n\ndigraph G {");

		// Create external keys for nodes
		for (int i = 0; i < cells.length; i++)
			if (JGraphpadCodecPlugin.isVertex(graph, cells[i]))
				hash.put(cells[i], new Integer(hash.size()));

		// Process Nodes
		Iterator it = hash.keySet().iterator();
		while (it.hasNext()) {
			Object node = it.next();
			gv.append(encodeVertex(graph, hash.get(node), node));
		}

		// Process Edges
		int edges = 0;
		for (int i = 0; i < cells.length; i++)
			if (graph.getModel().isEdge(cells[i]))
				gv.append(encodeEdge(graph, new Integer(edges++), cells[i]));

		// Close main tags
		gv.append("\n}");
		return gv.toString();
	}

	private static String encodeVertex(JGraph graph, Object id, Object vertex) {
		if (id == null)
			return "";
		String label = graph.convertValueToString(vertex);
		if (label == null)
			label = "";
		return "\n\t" + id.toString() + " [label=\"" + label + "\", "
				+ "shape=\"box\"];";
	}

	private static String encodeEdge(JGraph graph, Object id, Object edge) {
		GraphModel model = graph.getModel();
		String from = null;
		Object es = model.getSource(edge);
		if (es != null) {
			Object ps = hash.get(model.getParent(es));
			if (ps != null)
				from = ps.toString();
			// debug if (es != null)
			// from = graph.convertValueToString(model.getParent(es));
			String to = null;
			Object et = model.getTarget(edge);
			if (et != null) {
				Object tp = hash.get(model.getParent(et));
				if (tp != null)
					to = tp.toString();
				// debug if (et != null)
				// to = graph.convertValueToString(model.getParent(et));
			}
			if (from != null && to != null)
				return "\n\t" + from + " -> " + to + ";";
		}
		return "";
	}

}
